package tankgame03_bullet;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

public class MyPanle extends JPanel implements KeyListener,Runnable {
    Hero hero = null;//定义自己tank
    Vector<EnemyTank> enemyTanks=new Vector<>();//定义敌人TANk
    int enemyTankSize = 3;//定义敌人Tank数量


    public MyPanle(){
        //初始话自己的tank
        hero=new Hero(100,100);

        //初始话敌人Tank
        for (int i = 0; i < enemyTankSize; i++) {
            EnemyTank enemyTank = new EnemyTank(200 * i+100, 0);
            enemyTank.setDirct(2);
            enemyTank.setType(1);
            enemyTanks.add(enemyTank);
        }


    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0,0,1000,750);


        //画坦克
        drawTank(hero.getX(),hero.getY(),g,hero.getDirct(),hero.getType());
//        drawTank(x+100,y+100,g,dirct,type);
//        drawTank(300,300,g,1,1);
//        drawTank(400,300,g,2,1);
//        drawTank(500,300,g,3,1);
        //画敌人Tank
        for (EnemyTank enemyTank:enemyTanks
             ) {
            drawTank(enemyTank.getX(),enemyTank.getY(),g,enemyTank.getDirct(),enemyTank.getType());
        }
        //画Bullet
        if (hero.getBullet()!=null&&hero.getBullet().isIslive()){
            g.setColor(Color.red);
            g.fill3DRect(hero.getBullet().getX(),hero.getBullet().getY(),1,1,true);
        }



    }
    //画坦克方法
    public void drawTank( int x,int y,Graphics g,int dirct,int type){
        switch (type){
            case 0://自己的tank
                g.setColor(Color.cyan);
                break;
            case 1://敌人的tank
                g.setColor(Color.yellow);
                break;
        }
        switch (dirct){
            case 0: //向上
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x+30,y,10,60,false);
                g.fill3DRect(x+10,y+10,20,40,false);
                g.fillOval(x+10,y+20,20,20);
                g.drawLine(x+20,y,x+20,y+20);
                break;
            case 1://右
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y+30,60,10,false);
                g.fill3DRect(x+10,y+10,40,20,false);
                g.fillOval(x+20,y+10,20,20);
                g.drawLine(x+40,y+20,x+60,y+20);
                break;


            case 2://下
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x+30,y,10,60,false);
                g.fill3DRect(x+10,y+10,20,40,false);
                g.fillOval(x+10,y+20,20,20);
                g.drawLine(x+20,y+20,x+20,y+60);
                break;
            case 3://左
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y+30,60,10,false);
                g.fill3DRect(x+10,y+10,40,20,false);
                g.fillOval(x+20,y+10,20,20);
                g.drawLine(x+20,y+20,x,y+20);
                break;

            default:
                System.out.println("暂时未做处理");
        }





    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            if (hero.getDirct() != 0) {
                hero.setDirct(0);
            }
            hero.moveUp();
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            if (hero.getDirct() != 1) {
                hero.setDirct(1);
            }
            hero.moveRight();
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            if (hero.getDirct()  != 2) {
                hero.setDirct(2);
            }
            hero.moveDown();
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            if (hero.getDirct() != 3) {
                hero.setDirct(3);
            }
            hero.moveLeft();
        }
        if (e.getKeyCode() == KeyEvent.VK_J) {
            hero.shoot();
        }

        ///this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        this.repaint();
    }
    }
}
